package com.pp.tourism.web.task;

import com.pp.tourism.web.pojo.ScenicSpot;
import com.pp.tourism.web.pojo.status.ScenicStatus;
import com.pp.tourism.web.service.ScenicSpotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

import static com.pp.tourism.web.pojo.status.ScenicStatus.CLOSED;
import static com.pp.tourism.web.pojo.status.ScenicStatus.MAINTAINANCE;
import static com.pp.tourism.web.pojo.status.ScenicStatus.OPEN;

@Slf4j
@Component
public class ScenicStatusTask {

    @Autowired
    private ScenicSpotService scenicSpotService;

//    @Scheduled(cron = "")
    public void changeScenicSpotState() {
        LocalTime now = LocalTime.now();
        List<ScenicSpot> scenicSpots = scenicSpotService.queryAllScenics();
        for (ScenicSpot scenicSpot : scenicSpots) {
            if (!MAINTAINANCE.toString().equals(scenicSpot.getScenicStatus())) {
                if (now.isAfter(LocalDateTime.ofInstant(scenicSpot.getScenicOpenTime().toInstant(), ZoneId.systemDefault()).toLocalTime()) && now.isBefore(LocalDateTime.ofInstant(scenicSpot.getScenicCloseTime().toInstant(), ZoneId.systemDefault()).toLocalTime())) {
                    scenicSpot.setScenicStatus(OPEN.toString());
                } else {
                    scenicSpot.setScenicStatus(CLOSED.toString());
                }
                log.info("景点: " + scenicSpot.getScenicName() + " 变更状态为: " + scenicSpot.getScenicStatus());
            }
        }
        scenicSpotService.updateBatchById(scenicSpots);
    }
}
